package com.ark.cloud.java.persistence.service.impl;

import com.ark.cloud.java.persistence.entity.BGCMD;
import com.ark.cloud.java.persistence.entity.BGCMDHEAD;
import com.ark.cloud.java.persistence.entity.BGParam;
import com.ark.cloud.java.persistence.entity.BgCmdBgParam;
import com.ark.cloud.java.persistence.mapper.BGCMDHEADMapper;
import com.ark.cloud.java.persistence.mapper.BGCMDMapper;
import com.ark.cloud.java.persistence.mapper.BGParamMapper;
import com.ark.cloud.java.persistence.mapper.BgCmdBgParamMapper;
import com.ark.cloud.java.persistence.service.BGCMDService;
import com.ark.cloud.java.persistence.utils.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

/**
 * @author fukun
 * @create 2021/9/1  上午10:40
 * @description
 */
@Service
public class BGCMDServiceImpl implements BGCMDService {
    @Autowired
    BGCMDMapper bgcmdDao;
    @Autowired
    BGCMDHEADMapper bgcmdheadDao;
    @Autowired
    BGParamMapper bgParamDao;
    @Autowired
    BgCmdBgParamMapper bgCmdBgParamDao;


    @Override
    public void saveBGCMDInfo(BGCMD bgcmd) {

        //                   设置UUID
        bgcmd.setId(UUIDUtil.next());
        System.out.println("bgcmd:"+bgcmd);

        // bgcmd_head表插入
        BGCMDHEAD bgCmdHead = bgcmd.getBgCmdHead();

        bgCmdHead.setId(UUIDUtil.next());
        if(bgCmdHead != null){
            bgcmdheadDao.insert(bgCmdHead);
        }


        Long bgCmdHeadId = bgCmdHead.getId();

        // bg_cmd表插入
        bgcmd.setBgheadId(bgCmdHeadId);

        bgcmdDao.insert(bgcmd);

        Long bgcmdId = bgcmd.getId();

        // bg_param、bg_cmd_bg_param表插入
        ArrayList<BGParam> paramArray = bgcmd.getParamArray();

        if(paramArray != null || paramArray.size()>0){
            for (BGParam bgParam : paramArray) {

                bgParam.setId(UUIDUtil.next());

                bgParamDao.insert(bgParam);
                Long bgParamId = bgParam.getId();

//            添加到中间表

                BgCmdBgParam bgCmdBgParam = new BgCmdBgParam();

                bgCmdBgParam.setId(UUIDUtil.next());
                bgCmdBgParam.setBgCmdId(bgcmdId);
                bgCmdBgParam.setBgParamId(bgParamId);

                bgCmdBgParamDao.insert(bgCmdBgParam);

            }
        }

    }
}
